package repository

import (
	"github.com/jinzhu/gorm"
	"mall-go/common"
	"mall-go/dto"
	"mall-go/model"
	"mall-go/util"
	"mall-go/vo"
	"strconv"
	"time"
)

type IndexConfigRepo struct {
	DB *gorm.DB
}

func NewIndexConfigRepo() IndexConfigRepo {
	Db := common.GetDB()
	return IndexConfigRepo{DB: Db}
}

func (receiver IndexConfigRepo) GetConfigGoodsesForIndex(configType, number int) []model.GoodsInfo {
	goodsRepo := NewMallGoodsRepo()
	indexConfigs := receiver.findIndexConfigsByTypeAndNum(configType, number)
	var goodes []model.GoodsInfo
	//如果不为空
	if len(indexConfigs) != 0 {
		//取出所有的goodsId
		var goodsIds []uint
		for _, config := range indexConfigs {
			goodsIds = append(goodsIds, config.GoodsId)
		}
		goodes = goodsRepo.findGoodsByIds(goodsIds)
	}
	return goodes
}

func (receiver IndexConfigRepo) findIndexConfigsByTypeAndNum(configType, number int) []model.IndexConfig {
	var indexConfig []model.IndexConfig
	receiver.DB.Where("config_type = ? and is_deleted = 0", configType).Order("config_rank desc").Limit(number).Find(&indexConfig)
	return indexConfig
}

func (receiver IndexConfigRepo) GetConfigsPage(pageDTO dto.IndexPageDTO) vo.IndexPageResult {
	var list []model.IndexConfig
	receiver.DB.Where("config_type = ? and is_deleted = 0", pageDTO.ConfigType).Order("config_rank desc").
		Limit(pageDTO.Limit).Offset(pageDTO.Start).Find(&list)

	var total int
	receiver.DB.Debug().Model(model.IndexConfig{}).Where("config_type = ? and is_deleted = 0", pageDTO.ConfigType).Count(&total)
	//封装分页数据
	page := vo.IndexPageResult{
		PageResult: vo.PageResult{
			TotalCount: total,
			PageSize:   pageDTO.Limit,
			TotalPage:  total / pageDTO.Limit,
			CurrPage:   pageDTO.Page,
		},
		List: list,
	}
	return page
}

func (receiver IndexConfigRepo) SaveIndexConfig(param dto.IndexConfigAddParam, id uint) bool {
	var tempGoods model.GoodsInfo
	receiver.DB.First(&tempGoods, param.GoodsId)
	if tempGoods.GoodsId == 0 {
		panic("数据不存在")
	}
	var tempIndex model.IndexConfig
	receiver.DB.Where("config_type = ? and goods_id = ? and is_deleted = 0", param.ConfigType, param.GoodsId).First(&tempIndex)
	if tempIndex.ConfigId != 0 {
		panic("数据已存在")
	}
	//执行添加操作
	//封装添加数据
	goodsId, _ := strconv.Atoi(param.GoodsId)
	configRank, _ := strconv.Atoi(param.ConfigRank)
	index := model.IndexConfig{
		ConfigName: param.ConfigName,
		ConfigType: uint(param.ConfigType),
		GoodsId:    uint(goodsId),
		ConfigRank: uint(configRank),
		CreateUser: id,
		UpdateUser: id,
	}
	if err := receiver.DB.Create(&index).Error; err != nil {
		panic("添加失败")
	}
	return true
}

func (receiver IndexConfigRepo) UpdateIndexConfig(param dto.IndexConfigEditParam, id uint) bool {
	var tempGoods model.GoodsInfo
	receiver.DB.First(&tempGoods, param.GoodsId)
	if tempGoods.GoodsId == 0 {
		panic("数据不存在")
	}
	var tempConfig model.IndexConfig
	receiver.DB.First(&tempConfig, param.ConfigId)
	if tempConfig.ConfigId == 0 {
		panic("数据不存在")
	}
	var tempIndex model.IndexConfig
	receiver.DB.Where("config_type = ? and goods_id = ? and is_deleted = 0", param.ConfigType, param.GoodsId).First(&tempIndex)
	if tempIndex.ConfigId != 0 && uint(param.ConfigId) != tempIndex.ConfigId {
		//goodsId相同且不同id，不能修改
		panic("无法修改")
	}
	//执行修改
	indexConfig := model.IndexConfig{
		ConfigId:   uint(param.ConfigId),
		ConfigName: param.ConfigName,
		ConfigType: uint(param.ConfigType),
		GoodsId:    uint(param.GoodsId),
		ConfigRank: uint(param.ConfigRank),
		UpdateTime: model.Time(time.Now()),
		UpdateUser: id,
	}
	if err := receiver.DB.Model(&model.IndexConfig{}).Update(indexConfig).Error; err != nil {
		panic("修改失败")
	}
	return true
}

func (receiver IndexConfigRepo) DeleteBatch(param dto.BatchIdParam, id uint) bool {
	if err := receiver.DB.Debug().Model(&model.IndexConfig{}).
		Where("config_id in (" + util.Convert(param.UserIds) + ")").
		Update(model.IndexConfig{IsDeleted: 1, UpdateUser: id, UpdateTime: model.Time(time.Now())}).Error; err != nil {
		panic("删除失败")
	}
	return true
}
